package com.aw.guiguzi.module.manager._user

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwFormatCheckUtils
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.databinding.ActivityManagerUserCreateBinding
import com.aw.guiguzi.event.EventRefreshUserListType
import com.aw.guiguzi.module.manager._user.presenters.UserCreatePresent
import com.aw.guiguzi.module.manager._user.views.UserCreateView

class UserCreateActivity :
    AwMvpBindingActivity<UserCreatePresent?, ActivityManagerUserCreateBinding?>(),
    UserCreateView.View {
    private var isOpenEye = false
    override fun createPresenter(): UserCreatePresent {
        return UserCreatePresent(this)
    }

    override fun getViewBinding(): ActivityManagerUserCreateBinding {
        return ActivityManagerUserCreateBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        binding?.etPwd?.transformationMethod = PasswordTransformationMethod.getInstance()
        setToolbar("新增用户", null)
    }

    override fun initData() {
        super.initData()
    }

    override fun initListener() {
        super.initListener()
        binding?.ivEye!!.setOnClickListener { v: View? ->
            if (isOpenEye) {
                binding?.ivEye!!.setImageResource(R.drawable.icon_eye_off)
                isOpenEye = false
                binding?.etPwd!!.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding?.ivEye!!.setImageResource(R.drawable.icon_eye_on)
                isOpenEye = true
                binding?.etPwd!!.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            binding?.etPwd!!.setSelection(getText(binding?.etPwd).length)
        }
        findViewById<View>(R.id.tv_confirm).setOnClickListener { v: View? ->
            if (AwDataUtil.isEmpty(getText(binding?.etUsername))) {
                showMsg("请输入用户名")
                return@setOnClickListener
            }
            if (!AwDataUtil.isEmpty(getText(binding?.etUserPhone)) && getText(binding?.etUserPhone).length != 11) {
                showMsg("手机号长度不符")
                return@setOnClickListener
            }
            if (AwDataUtil.isEmpty(getText(binding?.etNickname))) {
                showMsg("请输入昵称")
                return@setOnClickListener
            }
            if (AwDataUtil.isEmpty(getText(binding?.etPwd))) {
                showMsg("请输入密码")
                return@setOnClickListener
            }
            if (getText(binding?.etPwd).length < 6) {
                showMsg("密码长度不可小于6位")
                return@setOnClickListener
            }
            if (!AwFormatCheckUtils.isLetterDigitAll(getText(binding?.etPwd))) {
                showMsg("密码需包含字母与数字")
                return@setOnClickListener
            }
            val bean = UserInfoBean()
            if (!AwDataUtil.isEmpty(getText(binding?.etUserPhone))) {
                bean.mobile = getText(binding?.etUserPhone)
            }
            bean.username = getText(binding?.etNickname)
            bean.nickname = getText(binding?.etNickname)
            bean.password = getText(binding?.etPwd)
            bean.status = "0"
            bean.postIds = ArrayList()
            bean.roleIds = ArrayList()
            mPresenter!!.createUser(bean)
        }
    }

    override fun userRoleAssignSuccess() {
        showMsg("用户创建成功")
        postEvent(EventRefreshUserListType(null))
        finish()
    }

    override fun userRoleAssignFail(code: Int, msg: String?) {
        postEvent(EventRefreshUserListType(null))
        showDialogToFinish("用户创建成功，但角色分配失败，请在管理后台尝试操作")
    }
}